import {Switch,Route,Redirect} from 'react-router-dom';

import React from 'react';

import {RouteItemProp} from './type.d';

interface Iprop{
    routes:RouteItemProp[]
}

const RouterView:React.FC<Iprop> = ({routes}) => {
    let routeArr = routes.filter(item => item.component);
    let redirectArr = routes.filter(item => item.redirect);
    return <Switch>
        {
            routeArr.map(item => <Route path={item.path} key={item.path} render={(props) => {
                return <item.component {...props} routes={item.children}></item.component>
            }}></Route>)
        }     
        {
            redirectArr.map(item => <Redirect from={item.path} to={(item.redirect as string)} key={item.path}></Redirect>)
        }
    </Switch>
}

export default RouterView